/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.entity.AssetSupplier;
import com.fuyao.cloud.asset.vo.AssetSupplierMExcelVO;
import com.fuyao.cloud.asset.vo.FyAssetSupplierExcelVO;
import com.fuyao.cloud.asset.supplier.mapper.AssetSupplierMapper;
import com.fuyao.cloud.asset.supplier.service.AssetSupplierService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 供应商管理
 *
 * @author 时间的朋友
 * @date 2021-11-17 14:47:38
 */
@Service
public class AssetSupplierServiceImpl extends ServiceImpl<AssetSupplierMapper, AssetSupplier> implements AssetSupplierService {

    @Override
    public List<FyAssetSupplierExcelVO> listExcel(QueryWrapper queryWrapper) {
        List<AssetSupplier> supplierList = getSupplierList(queryWrapper);
        if (CollUtil.isNotEmpty(supplierList)) {
            return supplierList.stream().map(fyAssetSupplier -> {
                FyAssetSupplierExcelVO excelVO = new FyAssetSupplierExcelVO();
                BeanUtil.copyProperties(fyAssetSupplier, excelVO);
                return excelVO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    @Override
    public List<AssetSupplierMExcelVO> listExcelTypeM(QueryWrapper queryWrapper) {
        List<AssetSupplier> supplierList = getSupplierList(queryWrapper);
        if (CollUtil.isNotEmpty(supplierList)) {
            return supplierList.stream().map(fyAssetSupplier -> {
                AssetSupplierMExcelVO excelVO = new AssetSupplierMExcelVO();
                BeanUtil.copyProperties(fyAssetSupplier, excelVO);
                return excelVO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    @Override
    public List<Tree<Long>> selectTree(QueryWrapper<AssetSupplier> query) {
        // 查询型号标准信息
        List<AssetSupplier> supplierList = this.baseMapper.selectList(query);
        // 权限内房屋信息
        List<TreeNode<Long>> collect = supplierList.stream()
                .sorted(Comparator.comparing(AssetSupplier::getId)).map(sau -> {
                    TreeNode<Long> treeNode = new TreeNode();
                    treeNode.setId(sau.getId());
                    treeNode.setParentId(0L);
                    treeNode.setName(sau.getName());
                    treeNode.setWeight(120);
                    return treeNode;
                }).collect(Collectors.toList());
        return TreeUtil.build(collect, 0L);
    }

    private List<AssetSupplier> getSupplierList(QueryWrapper queryWrapper) {
        return this.list(queryWrapper);
    }
}
